//
//  YBRequest.h
//  YBNetworking
//
//  Created by ybgo on 2018/11/22.
//  Copyright © 2018 嘉联支付有限公司. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "YBConst.h"

NS_ASSUME_NONNULL_BEGIN

@class YBUploadFormData;

@interface YBRequest : NSObject

+ (instancetype)request;

/// YBRequest 对象 唯一标识 当请求发送时，由YBCenter分配
@property (nonatomic, copy, readonly) NSString *identifier;

/// 请求的服务器地址 例:"https://jlpay.com/mpos/" 默认为nil 如果‘useGeneralServer’是'YES(默认)',则在YBCenter中使用'generalServer'的值作为请求地址
@property (nonatomic, copy, nullable) NSString *server;

/// 请求地址路径 例:"service/login" 默认为nil
@property (nonatomic, copy, nullable) NSString *api;

/// 完整请求地址 例:"https://jlpay.com/mpos/service/login" 默认为nil 如果设置该请求地址会忽略 ‘server’和‘api’的值 无论是否设置
@property (nonatomic, copy, nullable) NSString *url;

/// 请求参数 (YBRequestEncryptNormal接口不加密/YBRequestParamatersTypeDictinary) 如果'useGeneralParameters'是'YES(默认)',则在YBCenter中把'generalParameters'添加到'parameters'中
@property (nonatomic, strong, nullable) NSDictionary<NSString *, id> *parameters;

/// 请求参数 请求参数为NSData类型数据
@property (nonatomic, strong, nullable) NSData *dataParamater;

/// 请求头 如果'useGeneralHeaders'是'YES(默认)',则在YBCenter中把'generalHeaders'添加到'headers'中
@property (nonatomic, strong, nullable) NSDictionary<NSString *, NSString *> *headers;

/// 默认为'YES' ,在YBCenter中当'server'为nil时使用 'generalServer'
@property (nonatomic, assign) BOOL useGeneralServer;

/// 默认为'YES',在YBCenter中 是否将'generalHeaders'添加到'headers'中，默认添加
@property (nonatomic, assign) BOOL useGeneralHeaders;//公共请求头

/// 默认为'YES',在YBCenter中 是否将'generalParameters'添加到'parameters'(请求参数) (YBRequestEncryptNormal/YBRequestParamatersTypeDictinary 接口使用)
@property (nonatomic, assign) BOOL useGeneralParameters;//公共请求参数

/// 请求类型 Normal, Upload ,Download...
@property (nonatomic, assign) YBRequestType requestType;

/// 请求方法类型 YBHTTPMethodGET YBHTTPMethodPOST...
@property (nonatomic, assign) YBHTTPMethodType httpMethod;

/// 接口请求参数类型
@property (nonatomic, assign) YBRequestParamatersType paramsType;

/// 请求参数编码格式
@property (nonatomic, assign) YBRequestSerializerType requestSerializerType;

/// 请求返回的编码格式
@property (nonatomic, assign) YBResponseSerializerType responseSerializerType;

/// 超时时间 默认60S
@property (nonatomic, assign) NSTimeInterval timeoutInterval;

/// 当前请求失败重试次数 默认为0
@property (nonatomic, assign) NSUInteger retryCount;

/// 当前请求的 UserInfo 默认为nil generalUserInfo被使用
@property (nonatomic, strong, nullable) NSDictionary *userInfo;

/// 当前请求成功时回调
@property (nonatomic, copy, readonly, nullable) YBSuccessBlock successBlock;

/// 当前请求失败时回调
@property (nonatomic, copy, readonly, nullable) YBFailureBlock failureBlock;

/// 当前请求完成时回调
@property (nonatomic, copy, readonly, nullable) YBFinishedBlock finishedBlock;

/// 当请求为Upload和Download时 progress更新是回调
@property (nonatomic, copy, readonly, nullable) YBProgressBlock progressBlock;

/// 当一个请求完成时 将当前请求block置为nil 消除潜在的循环引用
- (void)cleanCallbackBlocks;

/// upload request 上传文件数据 默认为nil 当requestType 为‘YBRequestUpload’时使用
@property (nonatomic, strong, nullable) NSMutableArray<YBUploadFormData *> *uploadFormDatas;

/// 下载文件的保存路径 默认为nil 当requestType 为‘YBRequestDownload’时使用
@property (nonatomic, copy, nullable) NSString *downloadSavePath;

//上传文件时使用
- (void)addFormDataWithName:(NSString *)name fileData:(NSData *)fileData;
- (void)addFormDataWithName:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType fileData:(NSData *)fileData;
- (void)addFormDataWithName:(NSString *)name fileURL:(NSURL *)fileURL;
- (void)addFormDataWithName:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType fileURL:(NSURL *)fileURL;

@end


#pragma mark - YBUploadFormData

/// YBUploadFormData 上传文件时使用 详情看‘AFMultipartFormData’
@interface YBUploadFormData : NSObject

/// 指定数据关联的名称 不能为nil
@property (nonatomic, copy) NSString *name;

/// 文件名称 ‘Content-Disposition’ 不建议为nil
@property (nonatomic, copy, nullable) NSString *fileName;

/// 文件数据的类型 不建议为nil
@property (nonatomic, copy, nullable) NSString *mimeType;

/// “fileData”和“fileURL”中的任何一个都不应为“nil” 添加到表单中的数据 优先高于‘fileURL’
@property (nonatomic, strong, nullable) NSData *fileData;

/// 添加到表单中文件的url 如果fileData不为空 将忽略fileURL
@property (nonatomic, strong, nullable) NSURL *fileURL;

//创建对象
+ (instancetype)formDataWithName:(NSString *)name fileData:(NSData *)fileData;
+ (instancetype)formDataWithName:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType fileData:(NSData *)fileData;
+ (instancetype)formDataWithName:(NSString *)name fileURL:(NSURL *)fileURL;
+ (instancetype)formDataWithName:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType fileURL:(NSURL *)fileURL;

@end

NS_ASSUME_NONNULL_END
